
// 回显父级节点
export const changeDetSelect = function(key, treeData) {
  let arr = [] // 在递归时操作的数组
  let returnArr = [] // 存放结果的数组
  let depth = 0 // 定义全局层级
  // 定义递归函数
  function childrenEach(childrenData, depthN) {
    for (var j = 0; j < childrenData.length; j++) {
      depth = depthN // 将执行的层级赋值 到 全局层级
      arr[depthN] = childrenData[j]
      if (childrenData[j].id == key) {
        returnArr = arr.slice(0, depthN + 1) // 将目前匹配的数组，截断并保存到结果数组，
        break
      } else {
        if (childrenData[j].childrens) {
          depth++
          childrenEach(childrenData[j].childrens, depth)
        }
      }
    }
    return returnArr
  }
return childrenEach(treeData, depth)
}

// 处理菜单
export const dealMenu = function(treeData) {
  if (!treeData || treeData.length == 0) {
      return []
  }
  let res = []
  let map = {}
  for (let v of treeData) {
      v.children = []
      map[v.id] = v
  }
  for (let v of treeData) {
      let parent = map[v.parentId]
      if (parent) {
          if (!parent.children) parent.children = []
              parent.children.push(v)
          } else {
          res.push(v)
      }
  }
  return res
}

// 处理菜单找到显示页
export const findMenu = function(treeData) {
  let res = []
  if (treeData && treeData.length > 0) {
    treeData.forEach((el, index) => {
      if (index == 0) {
        if (el.children.length > 0) {
          res = findMenu(el.children)
        } else {
          res.push(el)
        }
      }
    })
  } 
  return res
}